Visual Basic 2003: Code  

Code: Reading Data from the Registry (Visual Basic)

This example reads the value of the "Name", in the current user's registry, under the key "Names".

Example

Dim key As Microsoft.Win32.RegistryKey
key = Microsoft.Win32.Registry.CurrentUser.OpenSubKey("Names")
Dim name As String = CType(key.GetValue("Name"), String)

Compiling the Code

Replace the "Names" parameter with the name of a key that exists directly under the HKEY_CURRENT_USER node of the registry.

Replace the "Name" parameter with the name of a value that exists directly under the "Names" node.

The GetValue method returns an Object. If you have Option Strict On in your code, you must cast the return value to the type of data that you previously put into the value.

Robust Programming

GetValue returns Nothing if the value does not exist.

When reading the registry from a Web application, the current user depends on the authentication and impersonation implemented in the Web application. To determine the current user in a Web application, see Code: Getting the User or Account Identity (Visual Basic).

The following conditions may cause an exception:

Security

Registry keys hold data, and all input data should be verified before it is used in an application.

See Also

Code: Deleting a Key from the Registry (Visual Basic) | Code: Creating a Key In the Registry (Visual Basic) | Registry Class | RegistryKey Class | CType Function | Code Access Security Basics | Visual Basic Code Example Topics